package com.chen.authserver.v1.service;

import com.chen.authserver.exception.ServiceException;
import com.chen.authserver.resp.SignInBody;
import com.chen.authserver.resp.SignUpBody;
import com.chen.authserver.utils.RSAUtils;
import com.chen.authserver.v1.repository.RsaRepository;
import com.chen.authserver.v1.repository.UserMysqlRepository;
import com.chen.jeneral.utils.StringUtils;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

import static com.chen.authserver.constant.RespCode.*;

@Slf4j
@Service
public class UserVerifyService {

    private String privateKey = null;

    private String publicKey = null;

    @Autowired
    private RsaRepository rsaRepository;

    @Autowired
    private UserMysqlRepository userMysqlRepository;

    private Gson gson = new Gson();

    @PostConstruct
    public void init() {
        privateKey = rsaRepository.findPrivateKey();
        publicKey  = rsaRepository.findPublicKey();
    }

    public SignInBody decodeLoginInfo(String loginInfo) throws ServiceException {
        try {
            String loginInfoDecode = rsaDecode(loginInfo);
            SignInBody signInBody = gson.fromJson(loginInfoDecode, SignInBody.class);
            return signInBody;
        } catch (Exception e) {
            log.error(e.toString());
            throw new ServiceException(LOGININFO_VERIFY_FAIL_CODE, LOGININFO_VERIFY_FAIL_MSG);
        }
    }


    public SignUpBody decodeSignUpInfo(String signUpInfo) throws ServiceException {
        try {
            String signUpInfoDecode = rsaDecode(signUpInfo);
            SignUpBody signUpBody = gson.fromJson(signUpInfoDecode, SignUpBody.class);
            return signUpBody;
        } catch (Exception e) {
            log.error(e.toString());
            throw new ServiceException(SIGNNFO_VERIFY_FAIL_CODE, SIGNINFO_VERIFY_FAIL_MSG);
        }
    }

    public boolean verifyAccountAndPwd(final String account, final String pwd) {
        String password = StringUtils.EncodeByMD5(pwd);
        String passwordInStore = userMysqlRepository.findPwd(account);
        return password.equals(passwordInStore);
    }


    /**
     * 密码解密
     *
     * */
    public String rsaDecode(final String content) throws Exception {
        byte[] decodePwdbase64 = Base64.decodeBase64(content);
        String decodePwd       = new String(RSAUtils.decryptByPrivateKey(decodePwdbase64, privateKey));
        return decodePwd;
    }

    public String rsaEncode(final String content) throws Exception {
        byte[] encodePwd       = RSAUtils.encryptByPublicKey(content.getBytes(), publicKey);
        String encodePwdBase64 = Base64.encodeBase64String(encodePwd);
        return encodePwdBase64;
    }

    public String getPrivateKey() {
        return privateKey;
    }

    public String getPublicKey() {
        return publicKey;
    }
}
